/**
 * 
 */
package grid;

import java.util.*;
import static grid.Constants.*;

/**
 * @author Darko
 * @author Dragan Sahpaski
 * 
 */
public class MigratorNode
        extends Node {

    int epoch;
    int migrationWidth;
    static final String CLASS = "grid.Migrator";
    String outputFiles;
    List<String> inputPopulationRelative;
    String logFile;

    public MigratorNode(int epoch, int migrationWidth) {
        this.epoch = epoch;
        this.migrationWidth = migrationWidth;
        this.name = "migrator" + "_" + epoch;
        logFile = "migrator" + (epoch + 1) + LOG_POSTFIX;
    }

    private void fillInputPopulationFiles() {
        inputPopulationRelative = new LinkedList<String>();
        for (int i = 0; i < inputNodes.size(); i++) {
            if (inputNodes.get(i) != null) {
                String breederNode = "root.nodes." + inputNodes.get(i).getName();
                String inputPopulationFile = breederNode + ".description.OutputSandbox[0]" + ", " + breederNode + ".description.OutputSandbox[1]";
                inputPopulationRelative.add(inputPopulationFile);
            }
        }
        outputFiles = "";
        for (int i = 0; i < inputNodes.size(); i++) {
            outputFiles += "\"population" + (i + 1) + "_" + (epoch + 1) + EXTENSION + "\", ";
        }
        outputFiles = outputFiles.substring(0, outputFiles.length() - 2);
    }

    public String getArguments() {
        if (inputPopulationRelative == null) {
            fillInputPopulationFiles();
        }
        return "\"" + ((argumentsPrefix != null) ? (argumentsPrefix + " ") : "") + CLASS + " " + epoch + " " + migrationWidth + " " + inputNodes.size() + "\"";
    }

    public String getInputSandbox() {
        if (inputPopulationRelative == null) {
            fillInputPopulationFiles();
        }
        StringBuilder inputDomain = new StringBuilder();
        inputDomain.append(inputPopulationRelative.get(0));
        for (int i = 1; i < inputPopulationRelative.size(); i++) {
            inputDomain.append(", " + inputPopulationRelative.get(i));
        }
        //return "{" + inputDomain + ", root.InputSandbox}";
        return "{\"" + this.getExecutable() + "\", root.InputSandbox}";
    }

    public String getOutputSandbox() {
        //return "{" + outputFiles + ", \"" + logFile + "\"}";
        return "{\"" + logFile + "\"}";
    }
}
